package com.SouthernWall_404.Age_Capitalism.common.API;

import com.SouthernWall_404.Age_Capitalism.AgeCapitalism;
import com.SouthernWall_404.Age_Capitalism.common.World.Items.WalletItem;
import net.minecraft.client.Minecraft;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;

public class WalletAPI {

    public static void showAmount(Player player,ItemStack itemStack)
    {
        WalletContains walletContains=read(itemStack);
        player.sendSystemMessage(Component.translatable("100:"+walletContains.amount_100));
        player.sendSystemMessage(Component.translatable("50:"+walletContains.amount_50));
        player.sendSystemMessage(Component.translatable("20:"+walletContains.amount_20));
        player.sendSystemMessage(Component.translatable("10:"+walletContains.amount_10));
        player.sendSystemMessage(Component.translatable("5："+walletContains.amount_5));
        player.sendSystemMessage(Component.translatable("1："+walletContains.amount_1));
        player.sendSystemMessage(Component.translatable("0.5:"+walletContains.amount_05));
        player.sendSystemMessage(Component.translatable("0.1:"+walletContains.amount_01));
    }

    /*
    从ItemStack中读取数据
    需要验证非空;
     */
    public static WalletContains read(ItemStack itemStack)
    {
        Item item=itemStack.getItem();//获取需要读取的物品
        if(item instanceof WalletItem walletItem)//是钱包类
        {
            WalletContains walletContains=readOrCreateFromItemStack(itemStack);//从中读取所含内容
            if(walletContains==null)//读不到
            {
                return null;//返回空
            }
            return walletContains;//若读到了，返回获取的内容
        }
        return null;
    }
    /*
    用于向itemStack中修改钱包数据。
     */
    public static void put(ItemStack itemStack,WalletContains walletContains) {
        if (Minecraft.getInstance() != null)
        {
            return;
        }
        CompoundTag nbt=itemStack.getOrCreateTag();//获取nbt
//        if(nbt.isEmpty())//空的
//        {
//            return ;//发生错误
//        }

        {
            CompoundTag modPack=createModPack(walletContains);//建立内容物数据包
            nbt.put(AgeCapitalism.MODID,modPack);//放入
        }
    }

    /*
用于将现有信息整合为modPack，方便输入nbt
 */
    public static CompoundTag createModPack(WalletContains walletContains)
    {
        CompoundTag modPack=new CompoundTag();
        modPack.putInt("100",walletContains.amount_100);
        modPack.putInt("50",walletContains.amount_50);
        modPack.putInt("20",walletContains.amount_20);
        modPack.putInt("10",walletContains.amount_10);
        modPack.putInt("5",walletContains.amount_5);
        modPack.putInt("1",walletContains.amount_01);
        modPack.putInt("05",walletContains.amount_05);
        modPack.putInt("01",walletContains.amount_01);
        return modPack;
    }
    /*
    建立一个空钱包
     */
    public static WalletContains initWalletContains()
    {
        return new WalletContains(
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0);
    }
    /*
    用于从钱包物品实例中
    读取已有的数据
    需要验证非空
     */
    public static WalletContains readOrCreateFromItemStack(ItemStack itemStack)
    {
        CompoundTag nbt=itemStack.getOrCreateTag();//获取nbt
//        if(nbt.isEmpty())//空的
//        {
//            return null;//发生错误
//        }
        //非空
        {
            CompoundTag modPack=(CompoundTag) nbt.get(AgeCapitalism.MODID);//获取模组信息包
            if(modPack==null)//如果模组信息包不存在
            {
                WalletContains newWallet=initWalletContains();//初始化一个空钱包
//                nbt.put(AgeCapitalism.MODID,createModPack(newWallet));//放入
                return newWallet;//返回
            }
            else//如果模组信息包存在
            {
                WalletContains ansWallet=initWalletContains();
                ansWallet.amount_100=modPack.getInt("100");
                ansWallet.amount_50=modPack.getInt("50");
                ansWallet.amount_20=modPack.getInt("20");
                ansWallet.amount_10=modPack.getInt("10");
                ansWallet.amount_5=modPack.getInt("5");
                ansWallet.amount_1=modPack.getInt("1");
                ansWallet.amount_05=modPack.getInt("05");
                ansWallet.amount_01=modPack.getInt("01");
                ansWallet.total=
                        ansWallet.amount_100*100+
                                ansWallet.amount_50*50+
                                ansWallet.amount_20*20+
                                ansWallet.amount_10*10+
                                ansWallet.amount_5*5+
                                ansWallet.amount_1*1+
                                ansWallet.amount_05*0.5f+
                                ansWallet.amount_01*0.1f;

                return ansWallet;//读取数据返回
            }
        }
    }
}
